import 'package:aitao_flutter/routes/routes.dart';
import 'package:aitao_flutter/utils/log_utils.dart';
import 'package:aitao_flutter/utils/sp_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:pin_code_fields/pin_code_fields.dart';
import '../../common_ui/back_nav.dart';
import '../../request/api.dart';
import '../../request/requestManager.dart';
import '../../style/box_style.dart';
import '../../style/color_style.dart';
import '../../style/edgeInsets_style.dart';
import '../../style/text_style.dart';

class YoundConfirmPsPage extends StatefulWidget {
  const YoundConfirmPsPage({super.key});

  @override
  State<YoundConfirmPsPage> createState() => _YoundConfirmPsPageState();
}

class _YoundConfirmPsPageState extends State<YoundConfirmPsPage> {
  TextEditingController editingController = TextEditingController();
  String password = '';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ColorStyle.whiteColor,
      appBar: BackNavigation(title: '青少年模式'),
      body: Container(
          alignment: Alignment.center,
          padding: EdgeInsetsStyle.customMargin(80, 0, 80, 100),
          child: ListView(
            children: [
              Box.vBox(60),
              Text(
                '确认密码',
                style: Styles.normalText_18,
                textAlign: TextAlign.center,
              ),
              Box.vBox20,
              PinCodeTextField(
                autoFocus: true,
                controller: editingController,
                cursorColor: ColorStyle.themeColor,
                keyboardType: TextInputType.number,
                onCompleted: (txt) {
                  editingController.text = txt;
                  if (editingController.text != password) {
                    EasyLoading.showToast('密码不一致');
                  }
                  setYoungPassword();
                },
                appContext: context, //context
                length: 4, //输入的长度
                onChanged: (value) {
                  editingController.text = value;
                }, //变化回调
              )
            ],
          )),
    );
  }

  @override
  void initState() {
    super.initState();
    password = Get.parameters['password'].toString();
  }

  void setYoungPassword() async {
    var result = await RequestManager()
        .post(API.setAdolescent, params: {'password': password});
    debugPrint('设置密码');
    WJLogUtils.wjLog(result);
    if (result != null && result['code'] == 0) {
      SpUtil.saveYoungModeStatus(true);
      Get.offAllNamed(Routes.tabPage);
    }
  }
}
